package com.googlecode.bubbler.api;

import java.io.File;

import edu.brandeis.cs.steele.exceptions.PronunciationNotFindException;
import edu.brandeis.cs.steele.exceptions.WordNotFindException;
import edu.brandeis.cs.steele.wn.DictionaryDatabase;
import edu.brandeis.cs.steele.wn.FileBackedDictionary;
import edu.brandeis.cs.steele.wn.IndexWord;
import edu.brandeis.cs.steele.wn.POS;
import edu.brandeis.cs.steele.wn.Pronunciation;
import edu.brandeis.cs.steele.wn.Synset;

public class LookApEngine {
    String word;
    private IndexWord wordIdx;

    public LookApEngine(String word) {
        this.word = word;
    }

    StringBuilder getNounSentens() throws WordNotFindException {
        return getSentensByPOS(POS.NOUN);
    }

    StringBuilder getVerbSentens() throws WordNotFindException {
        return getSentensByPOS(POS.VERB);
    }

    public Pronunciation getPronunciation() throws PronunciationNotFindException{
        return new Pronunciation(wordIdx);
    }

    private StringBuilder getSentensByPOS(POS pos) throws WordNotFindException {
        File f = new File(".//db");
        if (f.exists())
            System.out.println(".//db//index.noun");
        DictionaryDatabase dictionary = new FileBackedDictionary(".//db");
        IndexWord wordIdx = dictionary.lookupIndexWord(pos, word);
        if (wordIdx == null)
            throw new WordNotFindException();
        this.wordIdx = wordIdx;
        Synset[] senses = wordIdx.getSenses();
        int taggedCount = wordIdx.getTaggedSenseCount();
        StringBuilder wordBuild = new StringBuilder();
        // wordBuild.append(wordIdx.getLemma());
        wordBuild.append("The " + wordIdx.getPOS().getLabel() + " "
                + wordIdx.getLemma() + " has " + senses.length + " sense"
                + (senses.length == 1 ? "" : "s") + " \n");
        wordBuild.append("(");
        if (taggedCount == 0) {
            wordBuild.append("no senses from tagged texts");
        } else {
            wordBuild.append("first " + taggedCount + " from tagged texts");
        }
        wordBuild.append(")\n");
        for (int i = 0; i < senses.length; ++i) {
            Synset sense = senses[i];
            wordBuild.append("" + (i + 1) + ". " + sense.getLongDescription());

            wordBuild.append("\n");

        }
        return wordBuild;
    }

    public IndexWord getWordIdx() {
        return wordIdx;
    }

}
